package com.bc.bcapi.service;

import com.bc.bcapi.dao.T_User_browseMapper;
import com.bc.bcmodel.domain.T_User_browseModel;
import com.bc.bcmodel.enums.IntegralRecordEnum;
import com.bc.bcmodel.req.IntegralRecordReq;
import com.bc.bcmodel.resp.IntegralRecordResp;
import com.bc.bcmodel.resp.ProjectSearchInfoResp;
import com.bc.commonentity.common.JsonResult;
import com.bc.commonentity.log.LogBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.math.BigInteger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

import com.bc.bcapi.dao.T_Integral_recordMapper;
import com.bc.bcmodel.domain.T_Integral_recordModel;

/**
 * @Name T_Integral_recordService * @Description TODO * @Author generation * @Date 2018/8/9 15:56:31 * @Version 1.0
 **/
@Service
public class T_Integral_recordService {
    @Resource
    T_Integral_recordMapper t_Integral_recordMapper;
    @Resource
    T_User_browseMapper t_user_browseMapper;

    static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);

    /**
     * 保存单条数据     *      * @return
     */

    public int insert(T_Integral_recordModel req) {
        return t_Integral_recordMapper.insert(req);
    }

    /**
     * 批量增加数据     *      * @return
     */

    public int insertBatch(List<T_Integral_recordModel> req) {
        return t_Integral_recordMapper.insertBatch(req);
    }

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */

    public T_Integral_recordModel findByNamedParam(T_Integral_recordModel req) {
        return t_Integral_recordMapper.findByNamedParam(req);
    }

    /**
     * 根据对象查询所有数据     *      * @return
     */

    public List<T_Integral_recordModel> getAllList(T_Integral_recordModel req) {
        return t_Integral_recordMapper.findByNamedParamList(req);
    }

    /**
     * 根据对象查询所有分页数据     *      * @return
     */

    public PageInfo<T_Integral_recordModel> getList(T_Integral_recordModel req) {
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<T_Integral_recordModel> list = t_Integral_recordMapper.findByNamedParamList(req);
        return new PageInfo<T_Integral_recordModel>(list);
    }

    /**
     * 更新指定对象     *      * @return
     */

    public int update(T_Integral_recordModel req) {
        return t_Integral_recordMapper.update(req);
    }

    /**
     * 删除指定数据     *      * @return
     */

    public int delete(BigInteger id) {
        return t_Integral_recordMapper.delete(id);
    }

    /**
     * 根据条件统计数据量     *      * @return
     */

    public long count(T_Integral_recordModel req) {
        return t_Integral_recordMapper.count(req);
    }

    /**
     * 写积分记录
     * business_id=业务id
     * user_id：用户ID
     */
    public boolean insertIntegralRecord(IntegralRecordEnum em,
                                        String business_id, String user_id) {
        String item_info = em.key();
        int score = em.value();
        T_Integral_recordModel t_integral_recordModel = new T_Integral_recordModel();
        t_integral_recordModel.setUser_id(user_id);
        t_integral_recordModel.setItem_info(item_info);
        t_integral_recordModel.setBusiness_id(business_id);
        t_integral_recordModel.setScore(score);
        int result = t_Integral_recordMapper.insertRecord(t_integral_recordModel);
        if (result <= 0) {
            return false;
        }
        T_Integral_recordModel addJf = new T_Integral_recordModel();
        addJf.setUser_id(user_id);
        addJf.setScore(score);
        t_Integral_recordMapper.addIntegral(addJf);
        writeBusinessInfo(em, business_id, user_id);
        return true;
    }

    private void writeBusinessInfo(IntegralRecordEnum em,
                                   String business_id, String user_id) {
        if (em.LLICO.key().equals(IntegralRecordEnum.LLICO.key())) {
//记录用户浏览ICO
            T_User_browseModel t_user_browseModel = new T_User_browseModel();
            t_user_browseModel.setPid(business_id);
            t_user_browseModel.setUser_id(user_id);
            t_user_browseMapper.insert(t_user_browseModel);
        }
    }

    /**
     * 通过线程方式提交积分记录
     */
    public void threadInsertIntegralRecord(IntegralRecordEnum em,
                                           String business_id, String user_id) {
        fixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    insertIntegralRecord(em, business_id, user_id);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 获取积分记录
     */
    public JsonResult getIntegralRecordList(IntegralRecordReq req, String language) {
        JsonResult js = new JsonResult();
        T_Integral_recordModel model = new T_Integral_recordModel();
        model.setUser_id(req.getUser_id());
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<T_Integral_recordModel> list = t_Integral_recordMapper.getPage(model);
        if (language != null && language.equals("en")) {
            for (T_Integral_recordModel item :
                    list) {
                String item_info = item.getItem_info();
                List<IntegralRecordResp> listIntegral = new IntegralRecordResp().getList();
                List<IntegralRecordResp> temp = listIntegral.stream().
                        filter(t -> t.getZh_value().equals(item_info)).collect(Collectors.toList());
                if (temp.size() <= 0) {
                    continue;
                }
                item.setItem_info(temp.get(0).getEn_value());
            }
        }
        PageInfo<T_Integral_recordModel> pageInfo =
                new PageInfo<T_Integral_recordModel>(list);
        js.setData(pageInfo);
        return js;
    }
}